package com.elf.gwt.admin.client.components;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.admin.client.subcomponents.AccountLoginSubComponent;
import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.callback.SubComponentCallback;
import com.elf.gwt.common.client.model.AccountMetaModel;
import com.elf.gwt.common.client.model.AccountModel;
import com.elf.gwt.common.client.subcomponent.CommonSubComponent;
import com.elf.gwt.common.client.subcomponent.SwitchToSSLSubComponent;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.InlineLabel;

/**
 *  
 */
public class AccountComponent extends JSONServerComponent
{
	public static final String LoginAccount = "LoginAccount";
	public static final String PasswordForgotten = "PasswordForgotten";

	protected String action = "";
	protected String token = "";
	protected String subToken = "";

	protected AccountMetaModel accountMetaModel;
	protected AccountModel accountModel;

	protected AccountLoginSubComponent loginForm;


	/**
	 * Constructor
	 */
	public AccountComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		accountModel = new AccountModel();
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		if (!CommonUtils.isSSL())
		{
			SwitchToSSLSubComponent ssl = new SwitchToSSLSubComponent(controller, controller.getHistoryToken(), "");
			add(ssl);
			ssl.submit();
		}

		action = "";
		token = "";
		subToken = "";

		if (params.length > 1) action = params[1];
		controller.hideComponents();
		if (action.equals(LoginAccount)) 
		{
			if (params.length > 2) {
				subToken = params[2];
				token = "AccountComponent--" + action + "--" + subToken;
			}
			else {
				token = "AccountComponent--" + action;
			}
			try {buildFromModel();}catch(Exception e){}
			return false;
		}

		return true;
	}

	public void additionalRequestParameters()
	{
		if (action.equals(PasswordForgotten))
		{
			addRequestParameter(new RequestParameter("email_address", accountModel.getEmailAddress()));
		}
		if (action.length() > 0)
			addRequestParameter(new RequestParameter("action", action));
	}

	protected void buildMetaModel(JSONObject jsonObject)
	{
		accountMetaModel = new AccountMetaModel(jsonObject);
	}

	protected void buildModel(JSONObject jsonObject)
	{
		accountModel.update(jsonObject);
		controller.setLoggedIn(accountModel.getId() > 0);
	}

	public void buildFromModel() throws Exception
	{	
		if (accountModel.getId() < 1)
		{
			if (!(action.equals(LoginAccount))) action = LoginAccount;
			clear();
			accountModel.setHistoryAction(LoginAccount);
			buildLoginAccount();
		}
		else
		{			
			setVisible(false);
		}
		onBuilt();
	}

	protected void buildLoginAccount() throws Exception
	{
		add(new InlineLabel(adminController.getAdminConstants().AccountComponentSigninTitle()));

		if (loginForm == null)
		{
			loginForm = new AccountLoginSubComponent(
					adminController, 
					subComponentCallback);
		}
		loginForm.build();
		add(loginForm);
	}

	protected SubComponentCallback subComponentCallback = 
		new SubComponentCallback() {

		public void onNavigate(CommonSubComponent subComponent) {
			action = subComponent.getSuperAction();
			String successToken = getSubToken();
			if (successToken.length() > 0) successToken = "--" + successToken;
			onSelection("AccountComponent--" + action + successToken);
		}

		public void onSuccess(CommonSubComponent subComponent) {
			action = subComponent.getSuperAction();
			build(true);
		}

		public void onError(CommonSubComponent subComponent, Throwable exception) {}
	};

	public AccountModel getAccountModel() {
		return accountModel;
	}

	public AccountMetaModel getAccountMetaModel() {
		return accountMetaModel;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getToken() {
		return token;
	}

	public String getSubToken() {
		return subToken;
	}
}
